<?php
namespace Controller;

class ObjectControllerOutput {
	public $m_objectHtml = "";
}

class ObjectController {

	/**
	 * @return PostControllerOutput
	 */
	private static $edit = "edit";

	public function doControll(\Model\ObjectHandler $objecthandler, \Model\Database $database) {

		$object = \View\NavigationView::getSelectedObject($objecthandler, $database);
		$loginhandler = new \Model\LoginHandler($database);
		$objectView = new \View\ObjectView();
		//$commentController = new \Controller\CommentController();
		$out = new ObjectControllerOutput();
		$p_id = $object -> getId();
		$title = $object -> getTitle();
		$desc = $object -> getDesc();
		$message = "";

		//Om användaren är inloggad funkar allt detta
		if ($loginhandler -> IsLoggedIn() == true) {
			//Delete
			if ($objectView -> TriedToDelete() == true) {

				$messageid = $objecthandler -> DeleteObject($p_id);

				if ($messageid == true) {
					$message .= $objectView -> NewObjectMess(\View\ObjectView::OBJECT_DELETED);
					header('Location:index.php ');
				} else {
					$message .= $objectView -> NewObjectMess(\View\ObjectView::OBJECT_NOT_DELETED);
				}
			}
			//Om användaren trycker på redigera
			if ($objectView -> TriedToEdit() == true) {

				header('Location: index.php?objectID=' . $p_id . "&edit");
			}
			/*
			if (isset($_GET[self::$edit])) {

				$out -> m_objectHtml = $objectView -> EditObjectForm($object);

				if ($objectView -> TriedToEditSubmit() == true) {

					$messageid = $objecthandler -> EditPost($p_id, $objectView -> Title(), $objectView -> Content());

					if ($messageid == \Model\ObjectHandler::OBJECT_EMPTY) {
						$message .= $objectView -> NewObjectMess(\View\ObjectView::OBJECT_EMPTY);
					}
					if ($messageid == \Model\ObjectHandler::OBJECT_SAVED) {
						$message .= $objectView -> NewObjectMess(\View\ObjectView::OBJECT_SAVED);
						//header('Refresh: 1; URL=index.php?postID=' . $p_id);
						header('Location: index.php?postID=' . $p_id);
					}
					if ($messageid == \Model\ObjectHandler::OBJECT_FAILED) {
						$message .= $objectView -> NewObjectMess(\View\ObjectView::OBJECT_FAILED);
					}
					$out -> m_postHtml .= $message;

					if ($message != $objectView -> NewObjectMess(\View\ObjectView::OBJECT_SAVED)) {
						$out -> m_postHtml = $message;
						$out -> m_postHtml .= $objectView -> EditPostForm($object);

					}

					if ($message == $objectView -> NewObjectMess(\View\ObjectView::OBJECT_SAVED)) {
						$out -> m_postHtml .= $message;

						$out -> m_postHtml .= $objectView -> showObject($object, $database);

						header('Location: index.php?postID=' . $p_id);

					}
				}
				return $out;

			}
			 * 
			 */
		}
			 
		$out -> m_objectHtml .= $objectView -> showobject($object, $database);
		//$out -> m_postHtml .= $commentController -> DoControll($database, $p_id);
		return $out;
	}

}
